package org.apache.dolphinscheduler.api.controller;

import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.service.ProductService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.Product;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author denggh
 * @since 2023-06-15
 */
@Api(tags = "资源管理 API")
@RestController
@RequestMapping("product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    /**
     * 获取资源列表
     */
    @GetMapping("/list")
    @ApiOperation("获取资源列表")
    @PermissionCheck(permission = "operation:product:list")
    public Result list(
                       @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                       Product product,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("pageNo") Integer pageNo,
                       @RequestParam(value = "searchVal", required = false) String searchVal) {
        searchVal = ParameterUtils.handleEscapes(searchVal);
        return productService.selectPageProductList(product, pageSize, pageNo, searchVal, loginUser);
    }

    /**
     * 根据资源编号获取详细信息
     *
     * @param productId 资源ID
     */
    @GetMapping(value = "/{productId}")
    @ApiOperation("根据资源编号获取详细信息")
    @PermissionCheck(permission = "operation:product:config")
    public Result getInfo(@PathVariable Long productId) {
        Map<String, Object> result = productService.selectProductById(productId);
        return returnDataList(result);
    }

    /**
     * 新增资源
     */
    @PostMapping
    @ApiOperation("新增资源")
    public Result add(@Validated @RequestBody Product product) {
        Map<String, Object> result = productService.insertProduct(product);
        return returnDataList(result);
    }

    /**
     * 修改资源
     */
    @PutMapping
    @ApiOperation("修改资源")
    @PermissionCheck(permission = "operation:product:update")
    public Result edit(@Validated @RequestBody Product product) {
        Map<String, Object> result = productService.updateProduct(product);
        return returnDataList(result);
    }

    /**
     * 删除资源
     *
     * @param productId 资源ID
     */
    @DeleteMapping("/{productId}")
    @ApiOperation("删除资源")
    public Result remove(@PathVariable Long productId) {
        Map<String, Object> result = productService.deleteProductById(productId);
        return returnDataList(result);
    }

    /**
     * 同步资源
     */
    @PostMapping("/sync")
    @ApiOperation("同步资源")
    @PermissionCheck(permission = "operation:product:sync")
    public Result sync() {
        Map<String, Object> result = productService.syncProduct();
        return returnDataList(result);
    }
}
